package com.lmk.basic.service.Impl;

import com.lmk.basic.mapper.BaseMapper;
import com.lmk.basic.query.BaseQuery;
import com.lmk.basic.service.BaseService;
import com.lmk.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void add(T t) {
        mapper.save(t);
    }
    @Override
    @Transactional
    public void del(Long id) {
        mapper.remove(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        // 查询总条数
        Long count = mapper.queryCount(query);

        if (count < 1) {
            return new PageList<>();
        }
        // 进行分页查询
        List<T> list = mapper.queryDate(query);
        return new PageList<>(count, list);
    }

    @Override
    public void batchdel(List<Long> ids) {
        mapper.batchdel(ids);
    }
}
